/*
 * BusinessListDialog.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.ArrayRes;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.ui.adapter.BusinessDialogListAdapter;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.BusinessDialogListItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用的列表的Dialog
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/21 17:55
 */
public class BusinessListDialog extends Dialog {

    private BusinessListDialogController mController;

    private TextView mTvTitle;

    private TextView mTvMessage;

    private View mDivider;

    private ListView mListView;

    private BusinessDialogListAdapter mAdapter;

    public BusinessListDialog(Context context) {
        super(context, R.style.ThemeNoTitleDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_business_list);

        initViews();
        initListView();
    }

    private void initViews() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvMessage = (TextView) findViewById(R.id.tv_message);
        mListView = (ListView) findViewById(R.id.list_view);
        mDivider = findViewById(R.id.divider);
    }

    private void initListView() {
        mAdapter = new BusinessDialogListAdapter(getContext());
        mListView.setAdapter(mAdapter);
    }

    private void setController(BusinessListDialogController controller) {
        mController = controller;
    }

    @Override
    public void show() {
        super.show();

        if (mController == null) {
            throw new NullPointerException(
                    "You must use BusinessListDialog.Builder to create BusinessListDialog to show"
                            + " !");
        }

        setTitle(mController.getTitle());
        setMessage(mController.getMessage());
        refreshDividerUI(mController);
        setItems(mController.getItems(), mController.getOnClickListener());
    }

    private void refreshDividerUI(BusinessListDialogController controller) {
        if (TextUtils.isEmpty(controller.getTitle())
                && TextUtils.isEmpty(controller.getMessage())) {
            mDivider.setVisibility(View.GONE);
        }
    }

    private void setItems(String[] items, final OnClickListener listener) {

        if (items == null || items.length == 0) {
            return;
        }

        List<DisplayListItem> displayListItems = new ArrayList<>();

        for (String item : items) {
            displayListItems.add(new BusinessDialogListItem(item));
        }
        mAdapter.setData(displayListItems);
        mListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(
                            AdapterView<?> parent, View view, int position, long id) {
                        dismiss();
                        if (listener != null) {
                            listener.onClick(BusinessListDialog.this, position);
                        }
                    }
                });
    }

    private void setMessage(String message) {
        if (!TextUtils.isEmpty(message)) {
            mTvMessage.setVisibility(View.VISIBLE);
            mTvMessage.setText(message);
        }
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(title);
        }
    }

    public static class Builder {

        private BusinessListDialogController mController;

        public Builder(Context context) {
            mController = new BusinessListDialogController(context);
        }

        public Builder setTitle(String title) {
            mController.setTitle(title);
            return this;
        }

        public Builder setTitle(@StringRes int titleId) {
            return setTitle(mController.getContext().getString(titleId));
        }

        public Builder setMessage(String message) {
            mController.setMessage(message);
            return this;
        }

        public Builder setMessage(@StringRes int messageId) {
            return setMessage(mController.getContext().getString(messageId));
        }

        public Builder setItems(String[] items, DialogInterface.OnClickListener listener) {
            mController.setItems(items);
            mController.setOnClickListener(listener);
            return this;
        }

        public Builder setItems(@ArrayRes int itemsId, DialogInterface.OnClickListener listener) {
            return setItems(
                    mController.getContext().getResources().getStringArray(itemsId), listener);
        }

        public Builder setCancelable(boolean cancelable) {
            mController.setCancelable(cancelable);
            return this;
        }

        public BusinessListDialog create() {
            BusinessListDialog dialog = new BusinessListDialog(mController.getContext());
            dialog.setController(mController);
            return dialog;
        }
    }
}
